//
//  EMSortSegmentedControl.m
//  ElectronicMall
//
//  Created by fortsven on 15/2/26.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMSortSegmentedControl.h"

@implementation EMSortSegmentedControl{
    UIView *segmentBackView;
    NSMutableArray *selectIndexArray;
    CGRect rect;
    BOOL isIncrease;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        selectIndexArray = [NSMutableArray array];
        _titleArray = [NSArray array];
        rect = frame;
    }
    
    return self;
}
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self initializeSegmentView];
}
- (void)initializeSegmentView
{
    isIncrease = NO;
    
    segmentBackView = [[UIView alloc] initWithFrame:self.bounds];
    
    [self createSegmentItemWithNum:(int)_titleArray.count];
    
    [self addSubview:segmentBackView];
    
}

- (void)createSegmentItemWithNum: (int)num
{
    CGRect itemRect;
    itemRect.origin.x = 0;
    itemRect.origin.y = 0;
    itemRect.size.width = rect.size.width / num;
    itemRect.size.height = rect.size.height;
    
    for (int i = 0; i < num; i ++) {
        
        UIButton *segItem = [UIButton buttonWithType:UIButtonTypeCustom];
        segItem.frame = itemRect;
        
        CGRect lbRect = segItem.bounds;
        lbRect.origin.y = rect.size.height / 2 - 12;
        lbRect.size.height = 16;
        UILabel *label = [[UILabel alloc] initWithFrame:lbRect];
        label.text = _titleArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont fontWithName:HEITILIGHT size:16];
        [segItem addSubview:label];
        
        CGFloat stringWidth = [_titleArray[i] sizeWithFont:label.font].width;
        CGFloat x = label.bounds.size.width / 2 - stringWidth / 2;
        
        UIView *chooseView = [[UIView alloc] initWithFrame:CGRectMake(x, segItem.frame.size.height - 3, stringWidth, 3)];
        chooseView.backgroundColor = i == 0 ? RGB(113, 178, 81) : [UIColor clearColor];
        
        segItem.tag = 40150 + i;
        [selectIndexArray addObject:segItem];
        [segItem addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventTouchUpInside];
        [segItem addSubview:chooseView];
        
        if (i != 0) {
            CGRect aRect = segItem.frame;
            aRect.origin.x = segItem.frame.size.width - 15;
            aRect.origin.y = segItem.frame.size.height / 2 - 10;
            aRect.size.width = 15;
            aRect.size.height = 15;
            UIButton *sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            sortBtn.frame = aRect;
            [sortBtn setImage:[UIImage imageNamed:@"icon_排序"] forState:UIControlStateNormal];
            [segItem addSubview:sortBtn];

        }
        
        [segmentBackView addSubview:segItem];
        itemRect.origin.x += itemRect.size.width;
        
    }
    
    
    
}

- (void)segmentValueChanged: (UIButton *)sender
{
    self.selectedIndex = sender.tag - 40150;
    NSLog(@"%@", sender.subviews);
    for (UIView *btn in sender.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            btn.transform = CGAffineTransformRotate(btn.transform, M_PI);
        }
    }
    for (int i = 0; i < selectIndexArray.count; i ++) {
        UIView *select = selectIndexArray[i];
        UIView *selectedView = sender.subviews[1];
        
        
        if (sender.tag == select.tag) {
            
            selectedView.backgroundColor = RGB(113, 178, 81);
            NSMutableArray *lastArray = selectIndexArray;
            for (int j = 0; j < lastArray.count; j ++) {
                if (j != i) {
                    UIView *lastSelectView = [lastArray objectAtIndex:j];
                    UIView *lastSelect = lastSelectView.subviews[1];
                    lastSelect.backgroundColor = [UIColor clearColor];
                }
                
            }
            
        }
        
    }
    if (self.actionBlock)
        self.actionBlock(self.selectedIndex);
    
}

@end
